package com.cmol.peopledetectapp.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.cmol.data.SettingData;
import com.cmol.peopledetectapp.R;

/**
 * 系统设置界面
 *
 * @author 张瀚文
 */
public class SettingActivity extends AppCompatActivity {

    RadioGroup colorGroup, languageGroup;
    RadioButton blueButton, redButton, englishButton, chineseButton;
    TextView colorText, languageText, settingText;
    private int colorSet;
    private int languageSet;
    private Button okButton;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        colorText = findViewById(R.id.textView27);
        languageText = findViewById(R.id.textView4);
        settingText = findViewById(R.id.textView26);
        colorGroup = findViewById(R.id.ColorGroup);
        blueButton = findViewById(R.id.BlueButton);
        redButton = findViewById(R.id.RedButton);
        englishButton = findViewById(R.id.EnglishButton);
        chineseButton = findViewById(R.id.ChineseButton);
        view = findViewById(R.id.SettingBackground);

        colorSet = 0;
        languageSet = 0;
        languageGroup = findViewById(R.id.LanguageGroup);
        okButton = findViewById(R.id.OkButton);

        SystemSetting();

        /**
         * RadioGroup 按钮选择点击函数
         */
        colorGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.BlueButton) {
                    colorSet = 1;
                }
                if (checkedId == R.id.RedButton) {
                    colorSet = 2;
                }
            }
        });

        languageGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.ChineseButton) {
                    languageSet = 1;
                }
                if (checkedId == R.id.EnglishButton) {
                    languageSet = 2;
                }
            }
        });
    }

    /**
     * 确定按钮点击时修改全局设置参数
     *
     * @param view
     */
    public void SetClick(View view) {
        if (languageSet == 1) {
            SettingData.setLANGUAGE(true);
        }
        if (languageSet == 2) {
            SettingData.setLANGUAGE(false);
        }

        if (colorSet == 1) {
            SettingData.setCOLOR(true);
        }
        if (colorSet == 2) {
            SettingData.setCOLOR(false);
        }

        /**
         * 若有未选项提示信息
         */
        if (colorSet == 0 || languageSet == 0) {
            if (SettingData.getLANGUAGE() == true) {
                AlertDialog textTips = new AlertDialog.Builder(SettingActivity.this).setMessage("请先进行设置").create();
                textTips.show();
            } else {
                AlertDialog textTips = new AlertDialog.Builder(SettingActivity.this).setMessage("Please Set First").create();
                textTips.show();
            }

        } else {
            if (SettingData.getLANGUAGE() == true) {
                AlertDialog textTips = new AlertDialog.Builder(SettingActivity.this).setMessage("设置成功").create();
                textTips.show();
            } else {
                AlertDialog textTips = new AlertDialog.Builder(SettingActivity.this).setMessage("Successfully Set").create();
                textTips.show();
            }
        }
    }

    /**
     * 系统设置函数
     */
    private void SystemSetting() {
        if (SettingData.getCOLOR() == false) {
            okButton.setBackgroundResource(R.drawable.button_red);
            view.setBackgroundResource(R.drawable.redbackground);

        } else {
            okButton.setBackgroundResource(R.drawable.button_blue);
            view.setBackgroundResource(R.drawable.background_iner);
        }

        if (SettingData.getLANGUAGE() == false) {
            okButton.setText("OK");
            settingText.setText("System Setting");
            colorText.setText("Theme");
            languageText.setText("Language");
            blueButton.setText("Blue");
            redButton.setText("Red");
        } else {
            okButton.setText("确定");
            settingText.setText("系统设置");
            colorText.setText("主题");
            languageText.setText("语言");
            blueButton.setText("经典蓝色");
            redButton.setText("时尚红色");
        }
    }
}